package com.sanjiahantang.ythshopping.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.sanjiahantang.ythshopping.config.ConfigValue;
import com.sanjiahantang.ythshopping.http.BaseDelegate;
import com.sanjiahantang.ythshopping.http.OkHttpClientManager;
import com.squareup.okhttp.Request;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.umeng.socialize.facebook.controller.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;


public class WXEntryActivity extends Activity implements IWXAPIEventHandler{
	
	private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;
	
	// IWXAPI 是第三方app和微信通信的openapi接口
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
    	api = WXAPIFactory.createWXAPI(this, ConfigValue.WXAPP_ID , false);

        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	// 微信发送请求到第三方应用时，会回调到该方法
	@Override
	public void onReq(BaseReq req) {
		
		switch (req.getType()) {
		case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
			break;
		case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
			break;
		case ConstantsAPI.COMMAND_LAUNCH_BY_WX:
			break;
		default:
			break;
		}
	}

	// 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
	@Override
	public void onResp(BaseResp resp) {
		String code = "";
		if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
			//Toast.makeText(this, "code = " + (code = ((SendAuth.Resp) resp).code), Toast.LENGTH_SHORT).show();
			Log.e("WxAuth_onResp","code = " + (code = ((SendAuth.Resp) resp).code));
		}

		String result = "";
		Log.e("WxAuth_onResp","code = " + resp.errCode);
		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			if(resp instanceof SendAuth.Resp && TextUtils.equals("wechat_sdk_gym_login",((SendAuth.Resp)resp).state)){
				getWxId(code);
			}else{
				result="分享成功";
			}
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			result="分享取消";
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			result="认证失败";
			break;
		default:
			break;
		}

		if(resp instanceof SendAuth.Resp && (TextUtils.isEmpty(((SendAuth.Resp)resp).state)||TextUtils.equals("wechat_sdk_gym_login",((SendAuth.Resp)resp).state))){

		}else{
			Toast.makeText(this, result, Toast.LENGTH_LONG).show();
		}


		finish();
	}

	private void getWxId(String code){
		Map<String,String> params = new HashMap<>();
		String wx_url = "https://api.weixin.qq.com/sns/oauth2/access_token"
				+"?appid=" +  ConfigValue.WXAPP_ID
				+"&secret=" + ConfigValue.WXAPP_SECRET
				+"&grant_type=authorization_code"
				+"&code=" + code;

		OkHttpClientManager.getAsyn(this, wx_url,  new BaseDelegate.ResultCallback<String>() {
			@Override
			public void onError(Request request, Object tag, Exception e) {
				ToastUtil.showToast(WXEntryActivity.this,e.getMessage());
			}

			@Override
			public void onResponse(String response, Object tag) {
				ToastUtil.showToast(WXEntryActivity.this,response);
			}
		});
	}

}